import React from 'react';

const wrapEventHandler = (self, handlerName) => {
  const handler = self.props[handlerName];
  if (handler && typeof handler === 'function') {
    return (...args) => {
      handler.apply(self, args);
    };
  }
  return () => {};
};

const Counter = React.createClass({
  render() {
    return (
      <div>
        <p>
          <label>{this.props.counter}</label>
        </p>
        <div>
          <button onClick={this.handleIncreament}>INC</button>
          <button onClick={this.handleReset}>RST</button>
          <button onClick={this.handleRandom}>RND</button>
        </div>
      </div>
    );
  },

  handleIncreament() {
    wrapEventHandler(this, 'onIncrement')(2);
  },

  handleReset() {
    wrapEventHandler(this, 'onReset')();
  },

  handleRandom() {
    wrapEventHandler(this, 'onRandom')();
  }
});

export default Counter;
